<?php
/**
 * Created by PhpStorm.
 * User: 91336
 * Date: 13-12-14
 * Time: 上午10:44
 */
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_func('global', 'member');

class blanket
{
    public $db;
    public function __construct() {
        $this->db = pc_base::load_model('member_model');
    }
    /*
	 * 测试邮件配置
	 */
    public function send_newmail() {
        $_username = param::get_cookie('_regusername');
        $_userid = param::get_cookie('_reguserid');
        $newemail = $_GET['newemail'];
        if(empty($newemail)) exit('0');

        //验证是否已存在
        if($_userid){
            $status = $this->db->get_one("email = '{$newemail}' and userid != '{$_userid}'");
        }else{
            $status = $this->db->get_one(array('email'=>$newemail));
        }
        if($status) exit('-1'); //邮箱已存在

        //发送邮箱验证
        pc_base::load_sys_func('mail');
        $phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key'));
        $code = sys_auth($_userid.'|'.$phpcms_auth_key, 'ENCODE', $phpcms_auth_key);
        $url = APP_PATH."index.php?m=member&c=index&a=register&code=$code&verify=1";

        //读取配置获取验证信息
        $member_setting = getcache('member_setting');
        $message = $member_setting['registerverifymessage'];
        $message = str_replace(array('{click}','{url}','{username}','{email}','{password}'), array('<a href="'.$url.'">'.L('please_click').'</a>',$url,$_username,$newemail,$password), $message);

        if(sendmail($newemail, L('reg_verify_email'), $message)){
            //更新新的邮箱，用来验证
            $this->db->update(array('email'=>$newemail), array('userid'=>$_userid));
            exit('1');
        }else{
            exit('2');
        }
    }

    public function uploadavatar()
    {
        if(!isset($_GET['auth_data'])) exit('0');
        parse_str(sys_auth($_GET['auth_data'], 'DECODE'), $auth_data);
        $_userid = isset($auth_data['uid']) ? intval($auth_data['uid']) : exit('0');

        $dir1 = ceil($_userid / 10000);
        $dir2 = ceil($_userid % 10000 / 1000);
        $avatardata = $GLOBALS['HTTP_RAW_POST_DATA'];

        //创建图片存储文件夹
        $avatarfile = pc_base::load_config('system', 'upload_path').'avatar/';
        $dir = $avatarfile.$dir1.'/'.$dir2.'/'.$_userid.'/';
        if(!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        //存储flashpost图片
        $filename = $dir.$_userid.'.zip';
        file_put_contents($filename, $avatardata);

        pc_base::load_sys_func('dir');
        //解压缩文件
        pc_base::load_sys_class('pclzip', '', 0);
        $archive = new PclZip($filename);
        $archive->allow_ext = array('jpg');
        $list = $archive->extract(PCLZIP_OPT_PATH, $dir,PCLZIP_OPT_REMOVE_ALL_PATH);

        //判断文件安全，删除压缩包和非jpg图片
        $avatararr = array('180x180.jpg', '30x30.jpg', '45x45.jpg', '90x90.jpg');
        $files = glob($dir."*");
        foreach($files as $_files) {
            if(is_dir($_files)) dir_delete($_files);
            if(!in_array(basename($_files), $avatararr)) @unlink($_files);
        }
        if($handle = opendir($dir)) {
            while(false !== ($file = readdir($handle))) {
                if($file !== '.' && $file !== '..') {
                    if(!in_array($file, $avatararr)) {
                        @unlink($dir.$file);
                    } else {
                        $info = @getimagesize($dir.$file);
                        if(!$info || $info[2] !=2) {
                            @unlink($dir.$file);
                        }
                    }
                }
            }
            closedir($handle);
        }
        $this->db->update(array('avatar'=>1), array('userid'=>$_userid));
        exit('1');
    }
}